//
//  PersonalRegStep2ViewController.m
//  CarTown
//
//  Created by 熊浩 on 15/7/31.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "PersonalRegStep2ViewController.h"
#import "CheckboxButton.h"
#import "PickerTableViewController.h"
#import "CityPickerView.h"
#import <MBProgressHUD.h>
#import "PostUitls.h"
#import "ResMode.h"
#import "PersonalRegSUSViewController.h"
#import "WebViewController.h"

@interface PersonalRegStep2ViewController ()<CityPickerDelegate>{
    NSInteger postion;
}
@property (weak, nonatomic) IBOutlet CheckboxButton *agree_checkbox;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *truename;
@property (weak, nonatomic) IBOutlet UIButton *dizhi;
@property (weak, nonatomic) IBOutlet UITextField *company;
@property (weak, nonatomic) IBOutlet UIButton *xingzhi;
@property (nonatomic,strong) CityPickerView *piker;
@property (nonatomic,strong) NSArray *catidarray;
@property (nonatomic,assign) NSInteger ctid;
@end

@implementation PersonalRegStep2ViewController

-(void)citypicker:(NSString *)procestr city:(NSString *)citystr dist:(NSString *)dist{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    postion=0;
    self.password.secureTextEntry=YES;
    self.navigationItem.title=@"个人注册";
}
- (IBAction)hidepass:(id)sender {
    self.password.secureTextEntry=!self.password.secureTextEntry;
}
-(CityPickerView *)piker{
    if (_piker==nil) {
        _piker=[[CityPickerView alloc]initWithDelegate:self WithColoum:3];
    }
    return _piker;
}
- (IBAction)pushtoAgree:(id)sender {
    
    WebViewController *webvc=[WebViewController ViewController];
    webvc.type=Agreement;
    [self.navigationController pushViewController:webvc animated:YES];
    
}
-(void)pushToSUS:(ResMode *)mode{
    PersonalRegSUSViewController *psus=[PersonalRegSUSViewController viewcontoller];
    psus.mode=mode;
    [self.navigationController pushViewController:psus animated:YES];
}
-(void)citypicker:(NSString *)city{
    [self.dizhi setTitle:city forState:UIControlStateNormal];
    [self.dizhi setTitleColor:UIColorWithRGB(44, 44, 44)  forState:UIControlStateNormal];
}
- (IBAction)submit:(UIButton *)sender {
    if ([self CheckForm]) {
        
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在注册中请稍候";
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"action"]=Regparams;
        params[@"regtype"]=PersonTypeParams;
        params[@"username"]=self.phone;
        params[@"pass"]=self.password.text;
        params[@"truename"]=self.truename.text;
        if (self.company.text.length>0) {
            params[@"skype"]=self.company.text;
        }
        if (![self.xingzhi.titleLabel.text isEqualToString:@"请选择商户性质"]) {
            params[@"msn"]=[NSString stringWithFormat:@"%ld",self.ctid];
        }
        if (![self.xingzhi.titleLabel.text isEqualToString:@"请选择所在地址"]) {
            params[@"address"]=self.dizhi.titleLabel.text;
        }
      [PostUitls Post:hosturl parameters:params success:^(id response) {
          NSLog(@"%@",response);
          NSDictionary *dict=response;
          ResMode *mode=[ResMode modeWithDict:dict];
          if ([mode.state integerValue]==400) {
              NSLog(@"%@",mode.msg);
              [self pushToSUS:(mode)];
          }else{
              NSLog(@"%@",mode.msg);
              hud.labelText=mode.msg;
              hud.mode=MBProgressHUDModeText;
          }
          
          [hud hide:YES afterDelay:0.5];
      } failure:^(NSError *error) {
          NSLog(@"%@",error);
          hud.labelText=@"网络错误";
          hud.mode=MBProgressHUDModeText;
          [hud hide:YES afterDelay:1];
      }];
    }
   
}
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}



-(BOOL)CheckForm{
    
    [self.password endEditing:YES];
    [self.truename endEditing:YES];
    [self.company endEditing:YES];
    
    if (self.password.text.length<6) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            
            hud.labelText=@"密码必须大于等于6位";
            hud.mode = MBProgressHUDModeText;
            
            hud.margin = 10.f;
            
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];

        return false;
        
    }
    if (self.truename.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"真实姓名不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    if (self.agree_checkbox.isChecked==NO) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请同意用户协议";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return false;
    }
    return true;
}


- (IBAction)agreeclick:(CheckboxButton *)sender {
    self.agree_checkbox.isChecked=!self.agree_checkbox.isChecked;
    if ( self.agree_checkbox.isChecked) {
        [sender setImage:[UIImage imageNamed:@"circle1"] forState:UIControlStateNormal];
    }else{
        [sender setImage:[UIImage imageNamed:@"circle2"] forState:UIControlStateNormal];
    }
    
}
- (IBAction)xingzhiclick:(id)sender {
    PickerTableViewController *picker=[PickerTableViewController picker];
    picker.currtpostion=postion;
    picker.block=^(NSString *str,NSInteger ctid, NSInteger currtpositon){
        postion=currtpositon;
        [self.xingzhi setTitle:str forState:UIControlStateNormal];
        [self.xingzhi setTitleColor:UIColorWithRGB(44, 44, 44) forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:picker animated:YES];

}
- (IBAction)dizhi:(id)sender {
    [self.view becomeFirstResponder];
    [self.piker showinView:self.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.company endEditing:YES];
    [self.password endEditing:YES];
    [self.truename endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
